package com.usian.article.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.article.mapper.ArticleContentMapper;
import com.usian.article.service.ArticleConfigService;
import com.usian.article.service.ArticleService;
import com.usian.model.article.*;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.media.vos.WmNewsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/v1/article")
public class ApArticleController {
    @Autowired
    private ArticleService service;
    @Autowired
    private ArticleConfigService configService;

    @Autowired
    private ArticleContentMapper contentMapper;

    @PostMapping("save/about/article")
    public ApArticle saveArticleInfo(@RequestBody WmNewsVo news) {
        return service.saveArticleInfo(news);
    }

    @PostMapping("load/article/list")
    public ResponseResult loadArticleList(@RequestBody ArticleHomeDto dto){
        return service.load(dto);
    }

    @PostMapping("load/article/content/{id}")
    public ResponseResult loadContent(@PathVariable Long id){
        return service.loadContent(id);
    }

    @PostMapping("get/article/config")
    public ResponseResult getConfig(@RequestBody GetConfigDto dto){
        return configService.getConfig(dto);
    }

    @GetMapping("loadAll")
    public List<ArticleVo> loadAll(){
        List<ApArticle> list = service.list();
        List<ArticleVo> resultList = new ArrayList<>();
        for (ApArticle article : list) {
            ArticleVo vo = new ArticleVo();
            BeanUtils.copyProperties(article,vo);
            ApArticleContent articleContent = contentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, article.getId()));
            vo.setContent(articleContent !=null?articleContent.getContent():"");
            resultList.add(vo);
        }
        return resultList;
    }
}
